package com.leve.control.controller;

import com.leve.control.constant.Constants;
import com.leve.control.constant.ErrorCode;
import com.leve.control.domain.SetPermission;
import com.leve.control.service.SetPermissionService;
import com.leve.control.util.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author whongyu
 * @create by 2019/6/17
 */
@Slf4j
@RestController
@CrossOrigin
@RequestMapping("/api/setPermiss")
@Api(tags = {"pc:终端用户接口服务"})
public class SetPermissionController {

    @Autowired
    private SetPermissionService setPermissionService;

    @ApiOperation(value = "终端用户查询接口")
    @GetMapping(value = "/find")
    @ResponseBody
    public Result findSetPermission(@RequestBody SetPermission setPermission){
        log.info(Constants.REQ + ErrorCode.SUCCESS.getCode() + "|api/setPermiss/find|终端用户查询接口,setPermission={}: "+ setPermission);
        try{
            List<SetPermission> setPermissions = setPermissionService.findSetPermission(setPermission);
            return new Result(ErrorCode.SUCCESS,setPermissions);
        }catch (Exception e){
            log.error(Constants.RES + ErrorCode.E_10001.getCode()+ "|api/setPermiss/find|终端用户查询接口", e);
            return new Result(ErrorCode.E_10001);
        }
    }

    @ApiOperation(value = "终端用户添加接口")
    @PutMapping(value = "/add")
    @ResponseBody
    public Result addSetPermission(@RequestBody SetPermission setPermission){
        log.info(Constants.REQ + ErrorCode.SUCCESS.getCode() + "|api/setPermiss/add|终端用户添加接口,setPermission={}: "+ setPermission);
        try{
            int i = setPermissionService.addSetPermission(setPermission);
            if(i>0){
                return new Result(ErrorCode.SUCCESS);
            }else {
                return new Result(ErrorCode.E_20029);
            }
        }catch (Exception e){
            log.error(Constants.RES + ErrorCode.E_10001.getCode()+ "|api/setPermiss/add|终端用户添加接口", e);
            return new Result(ErrorCode.E_10001);
        }
    }

    @ApiOperation(value = "终端用户删除接口")
    @DeleteMapping(value = "/remove")
    @ResponseBody
    public Result deleteSetPermission(@RequestParam String setPId){
        log.info(Constants.REQ + ErrorCode.SUCCESS.getCode() + "|api/setPermiss/remove|终端用户删除接口,setPId={}: "+ setPId);
        try{
            int i = setPermissionService.deleteSetPermission(setPId);
            if(i>0){
                return new Result(ErrorCode.SUCCESS);
            }else {
                return new Result(ErrorCode.E_20029);
            }
        }catch (Exception e){
            log.error(Constants.RES + ErrorCode.E_10001.getCode()+ "|api/setPermiss/remove|终端用户删除接口", e);
            return new Result(ErrorCode.E_10001);
        }
    }

    @ApiOperation(value = "终端用户编辑接口")
    @PostMapping(value = "/edit")
    @ResponseBody
    public Result editSetPermission(@RequestBody SetPermission setPermission){
        log.info(Constants.REQ + ErrorCode.SUCCESS.getCode() + "|api/setPermiss/edit|终端用户编辑接口,setPermission={}: "+ setPermission);
        try{
            int i = setPermissionService.editSetPermission(setPermission);
            if(i>0){
                return new Result(ErrorCode.SUCCESS);
            }else {
                return new Result(ErrorCode.E_20029);
            }
        }catch (Exception e){
            log.error(Constants.RES + ErrorCode.E_10001.getCode()+ "|api/setPermiss/edit|终端用户编辑接口", e);
            return new Result(ErrorCode.E_10001);
        }
    }

}
